package com.hnevc.netdemo;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.nio.file.attribute.AclEntry.Builder;

public class UDPReceiverDemo {
	
	public static void main(String[] args) throws IOException  {
		DatagramSocket socket = new DatagramSocket(20000);//定义一个侦听端口
		byte[] buf = new byte[1024]; //数据缓存区
		DatagramPacket dp = new DatagramPacket(buf,buf.length);//构建接收数据的数据包
		socket.receive(dp); //侦听，接收数据包，存放到dp中
		//getData（）获取数据，dp.getLength()获取数据字节数，构建字符串
		String str = new String(dp.getData(),0,dp.getLength());
		System.out.println("接收数据");
		System.out.println(str+"  "+dp.getAddress() +":"+dp.getPort());
		socket.close();
	}
}
